<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar fixed border title="登录" left-text="更多" @click-left="$router.push('/')" left-arrow>
      <template #right>
        <van-icon name="search" size="18" @click="$router.push('/search')" />
      </template>
    </van-nav-bar>
    <div class="img"><img src="@/assets/touxiang/t0.jpg" alt="Alternate Text" /></div>
    <!-- /导航栏 -->
    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field v-model="user.mobile"
                 name="手机号"
                 placeholder="请输入手机号"
                 :rules="userFormRules.mobile"
                 type="number"
                 maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="user.password"
                 name="密码"
                 placeholder="请输入密码码"
                 :rules="userFormRules.password"
                 :type="pdwtype"
                 :right-icon="eye"
                 @click-right-icon="clickrighticon">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: 13824981159, // 手机号
        password: 'A123456789'
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        password: [{
          required: true,
          message: '密码不能为空'
        }, {
          pattern: /^(?![a-zA-Z]+$)(?![0-9]+$)[A-Za-z0-9]{8,18}$/,
          message: '密码格式错误'
        }]
      },
      pdwtype: 'password',
      eye: 'closed-eye'
    }
  },
  methods: {
    onSubmit () {
      const user = this.user
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 2000 // 持续时间，默认 2000，0 表示持续展示不关闭
      })
      if (user.mobile !== '13824981159' && user.password !== 'A123456789') {
        this.$toast.fail('手机号或密码错误')
      }
      this.$store.commit('setUser', user)
      setTimeout(() => {
        this.$router.push('/')
      }, 2000)
    },
    clickrighticon () {
      if (this.pdwtype === 'password') {
        this.pdwtype = 'number'
        this.eye = 'eye-o'
      } else {
        this.pdwtype = 'password'
        this.eye = 'closed-eye'
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
    .toutiao {
    font-size: 37px;
    }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #081d88;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
    background-color: #6db4fb;
    border: none;
    }
  }
  .van-form{
      margin-top:300px;
  }
  .img{
      text-align:center;
      position:absolute;
      top:100px;
      border-radius:100%;
      left:50%;
      transform:translateX(-50%);
      border:3px solid #081d88;
      img{
             border-radius:100%;
             height:180px;
             margin:10px;
         }
  }
}
</style>
